<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>易出图充值</title>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <script src="/layui/layui.all.js"></script>
    <script src="/js/core.util.js"></script>
    <script src="/js/jquery.js"></script>
    <script type="text/javascript">
        var openId = null;
        function load() {
            var params = {};
            CoreUtil.sendPost("/wxp/goodsList", params, function (res) {
                if (res.data != null && res.data.records != null) {
                    var goodsHtml = "";
                    //为每一行赋值
                    var recommend = true;
                    $.each(res.data.records, function (index, item) {
                        goodsHtml += "<a style=\"text-decoration:none\" onclick='jsApiCall({goodsId:\"" + item.id + "\"})'>";
                        if (recommend) {
                            goodsHtml += "  <div style=\"border-radius: 15px; background-color:#ffcc66; width:100%; height:80px; margin-bottom:20px;\">";
                            goodsHtml += "    <div style=\"float:left; font-size:22px; color:#FFFFFF; padding-left:20px;padding-top:25px;\"> 推荐: " + item.name + " </div>";
                            goodsHtml += "    <div style=\"float:right; font-size:22px; color:#FFFFFF; padding-right:20px;padding-top:25px;\"> " + item.price + "元 </div>";
                            goodsHtml += "  </div>";
                            recommend = false;
                        } else {
                            goodsHtml += "  <div style=\"border-radius: 15px; background-color:#CCCC33; width:100%; height:70px; margin-bottom:15px;\">";
                            goodsHtml += "    <div style=\"float:left; font-size:18px; color:#FFFFFF; padding-left:20px;padding-top:20px;\"> " + item.name + " </div>";
                            goodsHtml += "    <div style=\"float:right; font-size:18px; color:#FFFFFF; padding-right:20px;padding-top:20px;\"> " + item.price + "元 </div>";
                            goodsHtml += "  </div>";
                        }
                        goodsHtml += "</a>";
                    });
                    $(".goodsList").html(goodsHtml);
                }
            });
        }
        function getQueryVariable(variable) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i = 0; i < vars.length; i++) {
                var pair = vars[i].split("=");
                if (pair[0] == variable) { return pair[1]; }
            }
            return (false);
        }
        function jsApiCall(params) {
            params.code = getQueryVariable("code");
            params.openId = openId;
            CoreUtil.sendAJAX2("/wxp/order", JSON.stringify(params), function (res) {
                //初始化渲染数据
                if (res.data != null) {
                    openId = res.data.openId;
                    WeixinJSBridge.invoke(
                        'getBrandWCPayRequest',
                        res.data,
                        function (res) {
                            WeixinJSBridge.log(res.err_msg);
                            WeixinJSBridge.invoke('closeWindow', {}, function (res) { });
                        }
                    );
                }
            },
            function (err) {
                if(err.msg != undefined) {
                    alert(err.msg);
                } else {
                    alert("出错啦");
                }
                WeixinJSBridge.invoke('closeWindow', {}, function (res) { });
            },
            "POST");
        }
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', load, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', load);
                document.attachEvent('onWeixinJSBridgeReady', load);
            }
        } else {
            load();
        }
    </script>
</head>

<body>
<div class="goodsList"></div>
</body>

</html>